var util = require('../../utils/util.js');
Page({
  data: {
    // timer:'下班',
    constHours:'24',
    constMinutes:'60',
    constSeconds:'60',
    numh:'18',
    numm:'00',
    nums:'00',
    num:'1',
    currentTime: '',
    currentDownTime: '',
    dayMoney: '',
    workh:'8',
    workm:'0',
    works:'0',
    timeoutId:null,
    dayMoneyId:null,
    sMoney:'0.01213',
  },
  onLoad: function () {
    this.updateCurrentTime();
    setInterval(this.updateCurrentTime, 1000);
    this.updateCurrentDownTime();
    this.data.timeoutId = setInterval(this.updateCurrentDownTime, 1000);
    this.updateDayMoney();
    this.data.dayMoneyId = setInterval(this.updateDayMoney, 1000);
  },
  updateCurrentTime: function () {
    const now = new Date();
    const hours = now.getHours().toString().padStart(2, '0');
    const minutes = now.getMinutes().toString().padStart(2, '0');
    const seconds = now.getSeconds().toString().padStart(2, '0');
    this.setData({
      currentTime: `${hours}:${minutes}:${seconds}`
    });
  },
  updateCurrentDownTime: function () {
    const targetTime = new Date(new Date().getFullYear(),new Date().getMonth(),new Date().getDay(),this.data.numh, this.data.numm, this.data.nums);
    const currentTime = new Date();
    const timeDiff = targetTime - currentTime;
    const nnn = new Date(timeDiff-8*60*60*1000)
    const hours = nnn.getHours().toString().padStart(2, '0');
    const minutes = nnn.getMinutes().toString().padStart(2, '0');
    const seconds = nnn.getSeconds().toString().padStart(2, '0');
    this.setData({
      currentDownTime:  ` ${hours}:${minutes}:${seconds}`
    });
    if(hours<=0&&minutes<=0&&seconds<=0){
      wx.showToast({
        title: '下班了',
        icon: 'success',
        duration: 20000
      });
      clearTimeout(this.data.timeoutId);
    }
  },
  updateDayMoney: function () {
    const targetTime = new Date(new Date().getFullYear(),new Date().getMonth(),new Date().getDay(),this.data.workh, this.data.workm, this.data.works);
    const currentTime = new Date();
    const timeDiff =currentTime- targetTime;
    const nnn = new Date(timeDiff-8*60*60*1000)
    const hours = nnn.getHours().toString().padStart(2, '0');
    const minutes = nnn.getMinutes().toString().padStart(2, '0');
    const seconds = nnn.getSeconds().toString().padStart(2, '0');
    if(hours<=0&&minutes<=0&&seconds<=0){
      wx.showToast({
        title: '现在是下班时间',
        icon: 'success',
        duration: 20000
      });
      clearTimeout(this.data.timeoutId);
    }
    let total = (Number.parseInt(hours)*60*60+Number.parseInt(minutes)*60+Number.parseInt(seconds))*this.data.sMoney;
    this.setData({
      dayMoney: `${total.toFixed(2)}`
    });
  },
})

